//
//  PictureView.m
//  UI06_homework
//
//  Created by 洛洛大人 on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PictureView.h"
#import "LlScrollView.h"

@interface PictureView() <UIScrollViewAccessibilityDelegate>

@property(nonatomic, retain) LlScrollView *scrollView;
@property(nonatomic, retain) UIPageControl *pageControl;

@end

@implementation PictureView

-(void)dealloc {
    [_imageArray release];
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
    }

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        
        self.imageArray = [NSArray array];
        [self creatSubView];
        
    }
    return self;
    
}


//子视图
-(void)creatSubView {
    self.scrollView = [[LlScrollView alloc]initWithFrame:self.frame];
    self.scrollView.backgroundColor = [UIColor grayColor];
    [self addSubview:self.scrollView];
    [self.scrollView release];
    
    self.scrollView.contentOffset = CGPointMake(300, 0);
    //代理人
    self.scrollView.delegate = self;
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) / 5 * 3, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) / 5 * 2)];
    [self addSubview:self.pageControl];
    
    self.pageControl.numberOfPages = self.imageArray.count;
    
    [_pageControl release];
    
    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
}

-(void)pageAction:(UIPageControl *)pageControl {
    
    self.scrollView.contentOffset = CGPointMake(pageControl.currentPage * CGRectGetWidth(self.bounds), 0);
    
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.pageControl.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
    

    
    if (scrollView.contentOffset.x > CGRectGetWidth(self.bounds) * 4) {
        _scrollView.contentOffset = CGPointMake(300, 0);
    } else if(scrollView.contentOffset.x < CGRectGetWidth(self.bounds)){
     
        _scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds) * 4, 0);
        
    }
    
//    self.pageControl.currentPage = (scrollView.contentOffset.x - CGRectGetWidth(self.bounds)) / CGRectGetWidth(self.bounds);
    
    
}

    


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return scrollView.subviews.firstObject;
    
}



-(void)setImageArray:(NSArray<UIImage *> *)imageArray {
    if(_imageArray != imageArray) {
        [_imageArray release];
        
        _imageArray =[imageArray retain];
        
        
    }
    /**  添加图片  */
    [self.scrollView addPictures:_imageArray];
    self.pageControl.numberOfPages = _imageArray.count - 2;
    
}








@end
